package com.drug.lib.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.drug.lib.R;
import com.jakewharton.rxbinding2.view.RxView;

import java.util.concurrent.TimeUnit;


/**
 * Describe:公共的title
 *
 * @author coding
 * @date 2019/5/2
 */
public class CommonTitle extends LinearLayout {

    private ImageView imgRight;
    private ImageView imgLeft;
    private ImageView markIcon;

    private TextView tvTitle;
    private TextView tvLeft;
    private TextView tvRight;

    private LinearLayout layLeftView;
    private LinearLayout layRightView;
    private LinearLayout layTitleView;

    private RelativeLayout rlContainer;
    private View rlLine;

    private int mRightImage;
    private int mLeftImage;
    private int mTitleImage;


    private String mTitleText;
    private String mRightText;
    private String mLeftText;


    private int titleColor;
    private int rightColor;
    private int leftColor;
    private int lineColor;
    private int mBackgroundColor;

    private boolean isShowLine;
    private boolean isShowArrow;
    private boolean isShowLeft;
    private boolean isShowRight;
    private Context mContext;

    private CommonClickListener leftClickListener;
    private CommonClickListener rightClickListener;
    private CommonClickListener titleClickListener;

    public CommonTitle(Context context) {
        super(context);
        this.mContext = context;
        initView(context);
    }

    public CommonTitle(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView(context);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CommonTitle);
        mLeftImage = typedArray.getResourceId(R.styleable.CommonTitle_bar_image_left, R.mipmap.press_back);
        mTitleImage = typedArray.getResourceId(R.styleable.CommonTitle_bar_title_image, 0);
        mRightImage = typedArray.getResourceId(R.styleable.CommonTitle_bar_image_right, 0);

        mTitleText = typedArray.getString(R.styleable.CommonTitle_bar_title);
        mRightText = typedArray.getString(R.styleable.CommonTitle_bar_right_text);
        mLeftText = typedArray.getString(R.styleable.CommonTitle_bar_left_text);

        mBackgroundColor = typedArray.getColor(R.styleable.CommonTitle_bar_background_color, ContextCompat.getColor(getContext(), R.color.white));
        lineColor = typedArray.getColor(R.styleable.CommonTitle_bar_line_color, ContextCompat.getColor(getContext(), R.color.default_line_color));
        titleColor = typedArray.getColor(R.styleable.CommonTitle_bar_title_color, ContextCompat.getColor(getContext(), R.color.black));
        leftColor = typedArray.getColor(R.styleable.CommonTitle_bar_left_color, ContextCompat.getColor(getContext(), R.color.black));
        rightColor = typedArray.getColor(R.styleable.CommonTitle_bar_right_color, ContextCompat.getColor(getContext(), R.color.black));

        isShowLeft = typedArray.getBoolean(R.styleable.CommonTitle_bar_show_left, true);
        isShowRight = typedArray.getBoolean(R.styleable.CommonTitle_bar_show_right, false);
        isShowLine = typedArray.getBoolean(R.styleable.CommonTitle_bar_is_show_line, true);
        isShowArrow = typedArray.getBoolean(R.styleable.CommonTitle_bar_is_show_arrow, false);
        mImmersive = typedArray.getBoolean(R.styleable.CommonTitle_bar_is_immersive, false);
        typedArray.recycle();
    }

    private void initView(Context context) {
        View.inflate(context, R.layout.common_title, this);
        layLeftView = findViewById(R.id.lay_back);
        tvTitle = findViewById(R.id.tv_title);
        tvLeft = findViewById(R.id.left_text);
        imgRight = findViewById(R.id.img_right);
        tvRight = findViewById(R.id.tv_right_text);
        layRightView = findViewById(R.id.lay_right_button);
        rlContainer = findViewById(R.id.rl_container);
        imgLeft = findViewById(R.id.img_back);
        rlLine = findViewById(R.id.line_ll);
        layTitleView = findViewById(R.id.rl_title_bar);
        markIcon = findViewById(R.id.mark_icon);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        showRightView(isShowRight);
        showLeftView(isShowLeft);
        setImmersive(mImmersive);

        setTitleText(mTitleText);
        setRightText(mRightText);
        setLeftText(mLeftText);

        setRightImg(mRightImage);
        setLeftImg(mLeftImage);
        setTitleImage(mTitleImage);

        setTitleTextColor(titleColor);
        setLeftTextColor(leftColor);
        setRightTextColor(rightColor);

        setBackgroundColor(mBackgroundColor);
        setShowLine(isShowLine);
        rlLine.setBackgroundColor(lineColor);


        setEvent();
    }

    private void setEvent() {
        RxView.clicks(layLeftView)
                .throttleFirst(100, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    if (leftClickListener == null) ((Activity) mContext).finish();
                    else leftClickListener.onCommonClick();
                });
        RxView.clicks(layRightView)
                .throttleFirst(100, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    if (rightClickListener != null) rightClickListener.onCommonClick();
                });

        RxView.clicks(layTitleView)
                .throttleFirst(100, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    if (titleClickListener != null) titleClickListener.onCommonClick();
                });

    }

    public void setLeftClickListener(CommonClickListener leftClickListener) {
        this.leftClickListener = leftClickListener;
    }

    public void setRightClickListener(CommonClickListener rightClickListener) {
        this.rightClickListener = rightClickListener;
    }

    public void setTitleClickListener(CommonClickListener titleClickListener) {
        this.titleClickListener = titleClickListener;
    }

    private boolean mImmersive;
    private int mStatusBarHeight;

    public void setImmersive(boolean mImmersive) {
        this.mImmersive = mImmersive;
        if (mImmersive) {
            mStatusBarHeight = getStatusBarHeight();
        } else {
            mStatusBarHeight = 0;
        }
        setLayoutPadding();
    }

    public void setLayoutPadding() {
        rlContainer.setPadding(rlContainer.getPaddingLeft(), mStatusBarHeight + rlContainer.getPaddingTop(), rlContainer.getPaddingRight(), rlContainer.getPaddingBottom());
    }

    private static final String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height";

    /**
     * 计算状态栏高度高度
     * getStatusBarHeight
     *
     * @return
     */
    public static int getStatusBarHeight() {
        return getInternalDimensionSize(Resources.getSystem(), STATUS_BAR_HEIGHT_RES_NAME);
    }

    private static int getInternalDimensionSize(Resources res, String key) {
        int result = 0;
        int resourceId = res.getIdentifier(key, "dimen", "android");
        if (resourceId > 0) {
            result = res.getDimensionPixelSize(resourceId);
        }
        return result;
    }

    /**
     * 设置标题
     *
     * @param title 标题
     */
    public void setTitleText(String title) {
        this.mTitleText = title;
        tvTitle.setText(TextUtils.isEmpty(title) ? "" : title);
        tvTitle.setVisibility(TextUtils.isEmpty(title) ? GONE : VISIBLE);
    }

    /**
     * 设置右侧按钮文字
     *
     * @param text 文本
     */
    public void setRightText(String text) {
        this.mRightText = text;
        tvRight.setText(TextUtils.isEmpty(text) ? "" : text);
        tvRight.setVisibility(TextUtils.isEmpty(text) ? GONE : VISIBLE);
    }

    /**
     * 设置左侧按钮文字
     *
     * @param text 文本
     */
    public void setLeftText(String text) {
        this.mLeftText = text;
        tvLeft.setText(TextUtils.isEmpty(mLeftText) ? "" : mLeftText);
        tvLeft.setVisibility(TextUtils.isEmpty(mLeftText) ? GONE : VISIBLE);
    }

    /**
     * 设置标题栏右侧按钮图标
     *
     * @param resId 资源id
     */
    public void setRightImg(int resId) {
        this.mRightImage = resId;
        imgRight.setVisibility(resId == 0 ? GONE : View.VISIBLE);
        if (resId != 0)
            imgRight.setImageResource(resId);
    }

    /**
     * 设置标题栏右侧按钮图标
     *
     * @param resId 资源id
     */
    public void setTitleImage(int resId) {
        this.mTitleImage = resId;
        markIcon.setVisibility(resId == 0 ? GONE : View.VISIBLE);
        if (resId != 0)
            markIcon.setImageResource(resId);
    }

    /**
     * 设置标题栏左侧按钮图标
     *
     * @param resId 资源id
     */
    public void setLeftImg(int resId) {
        this.mLeftImage = resId;
        imgLeft.setVisibility(resId == 0 ? GONE : View.VISIBLE);
        if (resId != 0)
            imgLeft.setImageResource(resId);
    }

    public void showRightView(boolean show) {
        this.isShowRight = show;
        layRightView.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
    }

    public void showLeftView(boolean show) {
        this.isShowLeft = show;
        layLeftView.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
    }


    /**
     * 设置右按钮颜色
     *
     * @param color 颜色
     */
    public void setRightTextColor(int color) {
        this.rightColor = color;
        tvRight.setTextColor(color);
    }

    /**
     * 设置左按钮颜色
     *
     * @param color 颜色
     */
    public void setLeftTextColor(int color) {
        this.leftColor = color;
        tvLeft.setTextColor(color);
    }

    /**
     * 设置标题字体颜色
     *
     * @param color 颜色
     */
    public void setTitleTextColor(int color) {
        this.titleColor = color;
        tvTitle.setTextColor(color);
    }

    public void setBackgroundColor(int mBackgroundColor) {
        this.mBackgroundColor = mBackgroundColor;
        rlContainer.setBackgroundColor(mBackgroundColor);
    }

    public void setShowLine(boolean showLine) {
        isShowLine = showLine;
        rlLine.setVisibility(isShowLine ? VISIBLE : GONE);
    }

    public interface CommonClickListener {
        void onCommonClick();
    }
}
